package com.wzy.netty.nio;

import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * 描述  : 写入文件
 * 创建人:    Wzy
 * 创建时间: 2020-06-10  14:11
 */
public class NIOFileChannel01 {
    public static void main(String[] args) throws IOException {
        String str = "hello,world!netty实战";
        FileOutputStream outputStream = new FileOutputStream("01.txt");
        FileChannel channel = outputStream.getChannel();
        //创建缓冲
        ByteBuffer buffer = ByteBuffer.allocate(1024);
        buffer.put(str.getBytes());
        //再次写入 channel 需要翻转
        buffer.flip();
        // 数据从buffer写入到 buffer
        channel.write(buffer);
        outputStream.close();
    }
}
